package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategorMapper;
import com.sky.result.PageResult;
import com.sky.service.CategorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CategorServiceImpl implements CategorService {
    @Resource
    CategorMapper categorMapper;

    /**
     * 分类管理分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //设置页码，每页记录数
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //执行查询
        Page<Category> page = categorMapper.page(categoryPageQueryDTO);

        //返回数据
        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }

    /**
     * 启用禁用分类
     * @param category
     */
    @Override
    public void statusUpdate(Category category) {
        categorMapper.update(category);
    }

    /**
     * 新增分类
     * @param category
     */
    @Override
    public void add(Category category) {
        category.setStatus(0);
        categorMapper.add(category);
    }

    /**
     * 根据ID删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        categorMapper.delete(id);
    }

    /**
     * 修改分类
     * @param category
     */
    @Override
    public void update(Category category) {
        categorMapper.update(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> listPage(Integer type) {
        List<Category> category = categorMapper.listPage(type);
        return category;
    }
}
